package com.ktgj.service.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.domain.R;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Coupon;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.customer.api.domain.CustomerCard;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.service.domain.*;
import com.ktgj.service.domain.vo.*;
import com.ktgj.service.service.IVipchannelService;
import com.ktgj.service.service.impl.OrderViphallServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.service.service.IOrderVipchannelService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * VIP通道订单Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/ordervipchannels")
public class OrderVipchannelController extends BaseController
{
    @Autowired
    private IOrderVipchannelService orderVipchannelService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    @Autowired
    private IVipchannelService vipchannelService;

    @Autowired
    private RemoteFlightService remoteFlightService;

    /**
     * 查询VIP通道订单列表
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderVipchannel orderVipchannel)
    {
        startPage();
        List<OrderVipchannel> list = orderVipchannelService.selectOrderVipchannelList(orderVipchannel);
        return getDataTable(list);
    }

    /**
     * 导出VIP通道订单列表
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:export")
    @Log(title = "VIP通道订单 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderVipchannel orderVipchannel) throws IOException
    {
        List<OrderVipchannel> list = orderVipchannelService.selectOrderVipchannelList(orderVipchannel);
        ExcelUtil<OrderVipchannel> util = new ExcelUtil<OrderVipchannel>(OrderVipchannel.class);
        util.exportExcel(response, list, "VIP通道订单 数据");
    }

    /**
     * 获取VIP通道订单详细信息
     */
    @PreAuthorize(hasPermi = "service:vipchannel:query")
    @GetMapping(value = "/{ordervipchannelId}")
    public AjaxResult getInfo(@PathVariable("ordervipchannelId") Long ordervipchannelId)
    {
        return AjaxResult.success(orderVipchannelService.selectOrderVipchannelById(ordervipchannelId));
    }

    /**
     * 新增VIP通道订单
     */
    @PreAuthorize(hasPermi = "service:vipchannel:add")
    @Log(title = "VIP通道订单 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody OrderVipchannel orderVipchannel)
    {
        String token = request.getHeader("token");
        R<Customer> loginCustomerByToken = remoteCustomerService.getLoginCustomerByToken(token);
        orderVipchannel.setCustomerId(loginCustomerByToken.getData().getCustomerId());
        return toAjax(orderVipchannelService.insertOrderVipchannel(orderVipchannel));
    }

    /**
     * 修改VIP通道订单
     */
    @PreAuthorize(hasPermi = "service:vipchannel:edit")
    @Log(title = "VIP通道订单 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderVipchannel orderVipchannel)
    {
        return toAjax(orderVipchannelService.updateOrderVipchannel(orderVipchannel));
    }

    /**
     * 删除VIP通道订单
     */
    @PreAuthorize(hasPermi = "service:vipchannel:remove")
    @Log(title = "VIP通道订单 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ordervipchannelIds}")
    public AjaxResult remove(@PathVariable Long[] ordervipchannelIds)
    {
        return toAjax(orderVipchannelService.deleteOrderVipchannelByIds(ordervipchannelIds));
    }

    /**
     *快速通道详情
     */
    @GetMapping("/select")
    public AjaxResult select(Long ordervipchannelId) {
        return AjaxResult.success(orderVipchannelService.select(ordervipchannelId));
    }

    /**
     * VIP通道填写订单
     * @param servletRequest
     * @param request
     * @return
     */
    @PostMapping("/serviceInformation")
    public AjaxResult serviceInformation(HttpServletRequest servletRequest, @RequestBody(required = false) FillOrderRequest request) {
        String token = servletRequest.getHeader("token");
        VipchannelFillOrder fillOrder = orderVipchannelService.serviceInformation(token, request);
        return AjaxResult.success(fillOrder);
    }

    /**
     * 生成VIP通道订单
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/saveOrderItemVipchannel")
    public AjaxResult saveOrderItemVipchannel(HttpServletRequest request, @RequestBody(required = false) Map<String, Object> map) {
        String token = request.getHeader("token");
        Long vipchannelId = Long.valueOf(map.get("vipchannelId").toString());
        String arrivalDate = String.valueOf(map.get("arrivalDate"));
        Long peopleNumber = Long.valueOf(map.get("peopleNumber").toString());
        BigDecimal paymentAmount = new BigDecimal(map.get("paymentAmount").toString());
        BigDecimal points = new BigDecimal(map.get("points").toString());
        Long couponId = Long.valueOf(map.get("couponId").toString());
        BigDecimal totalsalePrice = new BigDecimal(map.get("totalsalePrice").toString());
        BigDecimal usedDeposit = new BigDecimal(map.get("usedDeposit").toString());
        String contactName = String.valueOf(map.get("contactName"));
        String contactPhone = String.valueOf(map.get("contactPhone"));
        String usePoint = map.get("usePoint").toString();
        String useDeposits = map.get("useDeposits").toString();
        String orderID;
        try {
            orderID = orderVipchannelService.saveOrder(token, vipchannelId, arrivalDate, peopleNumber, paymentAmount, points, couponId, totalsalePrice, usedDeposit, contactName, contactPhone, usePoint, useDeposits);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(orderID);
    }
    /**
     * VIP通道订单收银台
     * @param request
     * @return
     */
    @PostMapping("/queryByPayOrder")
    public AjaxResult queryByPayOrder(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        String token = request.getHeader("token");
        Long orderNumber = Long.valueOf(map.get("orderNumber").toString());
        String businessType = String.valueOf(map.get("businessType"));
        R<Customer> customer;
        try {
            customer = remoteCustomerService.getLoginCustomerByToken(token);
            return AjaxResult.success(orderVipchannelService.queryByPayOrder(customer.getData().getCustomerId(), orderNumber, businessType));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.expired("登录已过期，请重新登录");
        }
    }

    /**
     * 查询当前用户全部订单
     * */
    //@SignatureValidation
    @PostMapping("/selectVipAisleOrder")
    public AjaxResult selectVipAisleOrder(HttpServletRequest request, @RequestBody(required = false) Map<String, Object> map) {
        String type = null;
        if (map.get("type") != null && !map.get("type").equals("")){
            type = map.get("type").toString();
        }
        String token = request.getHeader("token");
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        Long customerId = customer.getData().getCustomerId();
        OrderVo orderVo;
        try {
            orderVo = orderVipchannelService.selectOrderVipchannelByCustomerIdAndOrdervipchannelId(customerId, type);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (orderVo != null){
            return AjaxResult.success(orderVo);
        }
        else {
            return AjaxResult.error("未查询到该用户的订单信息");
        }

    }
    /**
     * VIP通道支付成功页面
     * @param map
     * @return
     */
    @PostMapping("/selectOrderVipchannelNumber")
    public AjaxResult selectOrderVipchannelNumber(@RequestBody Map<String, Object> map) {
        Long ordervipchannelId = Long.valueOf(map.get("ordervipchannelId").toString());
        Map<String, Object> orderVipchannelNumber = orderVipchannelService.selectOrderVipchannelNumber(ordervipchannelId);
        return AjaxResult.success(orderVipchannelNumber);
    }

    /**
     * 查看行程用
     * @param ordervipchannelId
     * @return
     */
    @PostMapping("/queryVipchannelTrip")
    public Map<String, Object> queryVipchannelTrip(@RequestParam("ordervipchannelId") Long ordervipchannelId) {
        Map<String, Object> vipchannelTrip = orderVipchannelService.queryVipchannelTrip(ordervipchannelId);
        return vipchannelTrip;
    }

    /**
     * 查看行程（oederNumber）
     * @param map
     * @return
     */
    @PostMapping("/queryVipchannelTripByOrderNumber")
    public Map<String, Object> queryVipchannelTripByOrderNumber(@RequestBody  Map<String,Object>map) {
        Map<String, Object> vipchannelTrip = orderVipchannelService.queryVipchannelTripByOrderNumber(Long.valueOf(map.get("orderNumber").toString()));
        return vipchannelTrip;
    }

    /**
     * 定制行程-单程
     * @param customerId
     * @return
     */
    @PostMapping("/queryItineraryChannel")
    public AjaxResult queryItineraryChannel(@RequestParam("customerId") Long customerId) {
        OrderVipchannel vipchannel = orderVipchannelService.queryItinerary(customerId);
        if (!StringUtils.isEmpty(vipchannel))
            return AjaxResult.success(vipchannel);
        else
            return AjaxResult.error("无可定制行程的订单！");
    }

    @PostMapping("/selectItineraryChannel")
    public Map<String, Object> selectItineraryChannel(@RequestParam("ordervipchannelId") Long ordervipchannelId) {
        Map<String, Object> itinerary = orderVipchannelService.selectItinerary(ordervipchannelId);
        return itinerary;
    }

    /**
     * 微信小程序支付
     * @param map
     * @return
     */
    @PostMapping("/wechatPay")
    public AjaxResult wechatPay(@RequestBody Map<String, Object> map) {
        String openId = String.valueOf(map.get("openId"));
        Long orderNumber = Long.valueOf(map.get("orderNumber").toString());

        BigDecimal amount = new BigDecimal(0);
        if (map.get("amount") != null && !map.get("amount").equals("")){
            amount = new BigDecimal(map.get("amount").toString());
        }

        String type = map.get("type").toString();

        Map<String, Object> wechatPay;
        try {
            wechatPay = orderVipchannelService.wechatPay(openId, orderNumber, amount, type);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (wechatPay != null)
            return AjaxResult.success(wechatPay);
        else
            return AjaxResult.error("支付失败");
    }

    /**
     * 查询vip通道订单列表
     * */
    @PostMapping("/selectOrderVipchannel")
    public AjaxResult selectOrderVipchannel(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String type = null;
        if (map.get("type") != null && !map.get("type").equals("")){
            type = map.get("type").toString();
        }
        String token = request.getHeader("token");
        Long customerId = null;
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        if (customer.getCode() == 200 && customer.getData() != null)
            customerId = customer.getData().getCustomerId();
        else
            return AjaxResult.error("登录已过期！");

        List<OrderVipchannel> orderVipchannelList;
        try {
            orderVipchannelList = orderVipchannelService.selectOrderVipchannel(customerId, type);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (orderVipchannelList != null && orderVipchannelList.size() > 0){
            return AjaxResult.success(orderVipchannelList);
        }
        else {
            return AjaxResult.error("没有数据");
        }
    }

    /**
     * 获取用户vip通道订单数量
     */
    @PostMapping("/queryVipChannelOrderNumber")
    public int queryVipChannelNumber(@RequestBody Long customerId) {
        return vipchannelService.queryVipChannelNumber(customerId);
    }

    /**
     * 条件查询vip通道订单列表
     * */
    @PostMapping("/selectVipchannelOrederListVo")
    public TableDataInfo selectVipchannelOrederListVo(@RequestBody VipchannelOrederParameterVo vipchannelOrederParameterVo){
        startPage();
        List<OrderVipchannel> vipchannelOrederListVoList = orderVipchannelService.selectVipchannelOrederListVo(vipchannelOrederParameterVo);
        return getDataTable(vipchannelOrederListVoList);
    }

    /**
     * 条件查询vip通道订单列表
     * */
    @PostMapping("/VipchannelOrederListVo")
    public List<OrderVipchannel> VipchannelOrederListVo(@RequestBody VipchannelOrederParameterVo vipchannelOrederParameterVo){
        List<OrderVipchannel> vipchannelOrederListVoList = orderVipchannelService.selectVipchannelOrederListVo(vipchannelOrederParameterVo);
        return vipchannelOrederListVoList;
    }

    /**
     * vip通道订单详细
     * */

    @PostMapping("/selectVipchannelDetailsVo")
    public AjaxResult selectVipchannelOrederListDetailsVo(@RequestBody Map<String, Object> map) {
        Long ordervipchannelId = null;
        String orderNum = null;
        if (map.get("ordervipchannelId") != null && !map.get("ordervipchannelId").equals("")){
            ordervipchannelId = Long.valueOf(map.get("ordervipchannelId").toString());
        }
        else if (map.get("orderNum") != null && !map.get("orderNum").equals("")){
            orderNum = map.get("orderNum").toString();
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }

        OrderViphallDetailsVo orderViphallDetailsVo;
        try {
            orderViphallDetailsVo = orderVipchannelService.selectVipchannelOrederListDetailsVo(ordervipchannelId, orderNum);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (orderViphallDetailsVo != null){
            return AjaxResult.success(orderViphallDetailsVo);
        }
        else {
            return AjaxResult.error("未查到数据");
        }
    }

    /**
     * vip通道行程订单
     * */
    @PostMapping("/journeyOrderVipchannelVo")
    public JourneyOrderViphallVo journeyOrderVipchannelVo(@RequestBody Map<String, Object> map) {
        Long ordervipchannelId = Long.valueOf(map.get("ordervipchannelId").toString());
        JourneyOrderViphallVo journeyOrderViphallVo = orderVipchannelService.journeyOrderVipchannelVo(ordervipchannelId);
        return journeyOrderViphallVo;
    }

    /**
     * VIP通道退单
     * */
    @PostMapping("/chargebackVipchannel")
    public AjaxResult chargebackVipchannel(@RequestBody Map<String, Object> map) {
        if (map.get("ordervipchannelId") != null && !map.get("ordervipchannelId").equals("")){
            Long ordervipchannelId = Long.valueOf(map.get("ordervipchannelId").toString());
            int i = orderVipchannelService.chargebackVipchannel(ordervipchannelId);
            if (i > 0){
                return AjaxResult.success("退单成功");
            }
            else {
                return AjaxResult.error("退单失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
    /**
     * 订单号查数据
     * */
    @PostMapping("/queryByOrderNumber")
    public AjaxResult queryByOrderNumber(@RequestBody Map<String,Object> map) {
        if (map.get("orderNumber") != null && !map.get("orderNumber").equals("")){
            Long orderNumber = Long.valueOf(map.get("orderNumber").toString());
            OrderViphallDetailsVo orderViphallDetailsVo;
            try {
                orderViphallDetailsVo = orderVipchannelService.queryByOrderNumber(orderNumber);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (orderViphallDetailsVo != null){
                return AjaxResult.success(orderViphallDetailsVo);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 订单号获取vip通道订单信息
     * */
    @PostMapping("/orderNum")
    public OrderVipchannel orderVipchannel(@RequestBody Map<String, Object> map){
        String orderNum = map.get("orderNum").toString();
        OrderVipchannel orderVipchannel = orderVipchannelService.orderVipchannel(orderNum);
        return orderVipchannel;
    }
}
